package chapter7;

import java.util.stream.IntStream;
import java.util.stream.Stream;

public class SpliteratorDemo {

    public static void main(String[] args) {
        final String SENTENCE =
                " Nel   mezzo del cammin  di nostra  vita " +
                        "mi  ritrovai in una  selva oscura" +
                        " ché la  dritta via era   smarrita ";
        System.out.println("Found " + counWordsIteratively(SENTENCE) + " words");

        /**
         * 以函数式风格重写单词计数器
         */
        Stream<Character> stream = IntStream.range(0, SENTENCE.length()).mapToObj(SENTENCE::charAt);
    }

    public static  int counWordsIteratively(String s){
        int counter = 0;
        boolean lastSpace = true;
        for(char c: s.toCharArray()){
            if (Character.isWhitespace(c)){
                lastSpace = true;
            }else {
                if (lastSpace) counter++;
                lastSpace = false;
            }
        }
        return counter;
    }
}
